<?php

class AdminUsersController extends \AdminController {

	/**
	 * Display a listing of the resource.
	 *显示用户列表
	 * @return Response
	 */
	public function index()
	{
		$users = User::all();
		return View::make('admin.user.index',compact('users'));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
		return View::make('admin.user.create');
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$input = Input::all();
		unset($input['_token']);
		unset($input['avatar_image']);
		if(Input::hasFile('avatar_image')){
			$input['avatar'] = $this -> upload_one_file('avatar_image');
		}
		else $input['avatar'] = '';
		if(isset($input['status'])){
			$input['status'] = 0;
		}else $input['status'] = 1;
		if($input['password'] == $input['re_password']){
			unset($input['re_password']);
			$input['password'] = md5($input['password']);
		}else{
			unset($input['re_password']);
			echo '两次输入的密码不一样';
			return false;
		}
		$input['created_at'] = time();
		$input['updated_at'] = time();
		$input['last_login'] = 0;
		$result = User::insert($input);
		return Redirect::route('admin.user.index');

	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
		$user = User::find($id);
		return View::make('admin.user.edit',compact('user'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
		$input = Input::all();
		unset($input['_method']);
		unset($input['_token']);
		unset($input['avatar_image']);
		if(Input::hasFile('avatar_image')){
			$input['avatar'] = $this -> upload_one_file('avatar_image');
		}
		if(isset($input['status'])){
			$input['status'] = 0;
		}else $input['status'] = 1;
		if($input['password']!='')
			if($input['password'] == $input['re_password']){
				unset($input['re_password']);
				$input['password'] = md5($input['password']);
			}else{
				unset($input['re_password']);
				echo '两次输入的密码不一样';
				return false;
			}
		else {
			unset($input['password']);
			unset($input['re_password']);
		}
		$input['updated_at'] = time();
		$user = DB::table('users')->where('id',$id)->update($input);
		return Redirect::route('admin.user.index');
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
		return User::destroy($id);
		
	}


}
